using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CommonUtils;
using System.Diagnostics;

namespace ShellAnywhere.Model
{
	public class NavigationMgr : ActionMgr
	{
		#region verbs
		public static IVerb EMPTY = new Nav();
		#endregion verbs

		#region ctors
		public NavigationMgr()
		{
			_Lexer = new Lexer(_CharStream, _ParamTokenStream, true);
			_Parser = new Parser(this, _ParamTokenStream, _ActionTokenStream, EMPTY);

			IsActive = false;
		}
		#endregion ctors

		#region props
		public override string[] Targets { get { return new string [] { CurrentDirectory } ; } }
		#endregion props

		#region methods
		public override void Init()
		{
			_Lexer.AllowEmptyMatch = true;
			Load(CurrentDirectory);
		}
		public override void End()
		{
			
		}
		public override bool Advance()
		{
			DirectoryInfo di = new DirectoryInfo(Path.Combine(CurrentDirectory, Current.Name));
			if (di.Exists)
				CurrentDirectory = di.FullName;
			return false;
		}
		public override bool Withdraw()
		{
			DirectoryInfo di = new DirectoryInfo(CurrentDirectory);
			if (!string.IsNullOrEmpty(di.Parent.FullName))
				CurrentDirectory = di.Parent.FullName;
			return false;
		}
		public TargetList LoadDirectories(string[] targets)
		{
			TargetList list = new TargetList();
			if (!Common.IsNullOrEmpty(targets))
			{
				foreach (string directory in targets)
				{
					if (Directory.Exists(directory))
					{
						string[] folders = Directory.GetDirectories(directory);
						foreach (string folder in folders)
							list.Add(new Target(Path.GetFileNameWithoutExtension(folder)));
					}
				}
			}
			return list;
		}
		public override void Load(params string[] targets)
		{
			_Lexer.InitStateTree(LoadDirectories(targets));
			Update();
		}
		#endregion methods

		#region fields
		#endregion fields
	}
}
